package com.wildwolf.project.wildwolf.ui.register;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.wildwolf.project.wildwolf.R;
import com.wildwolf.project.wildwolf.config.AppConfig;

/**
 * 用户注册页面
 * 
 * @author LEON
 * 
 */
public class AccountActivity extends Activity implements View.OnClickListener {
	/**
	 * 返回页面
	 */
	private TextView mreturn = null;
	/**
	 * 下一页面
	 */
	private TextView next = null;

	/**
	 * 账户名
	 */
	private EditText accountName = null;
	/**
	 * 密码
	 */
	private EditText password = null;
	/**
	 * 确认密码
	 */
	private EditText surepassword = null;
	/**
	 * 
	 */
	private boolean isLegalAccountName = false, isLegalPassword = false,isLegalSurePassword = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.layout_reg_account);
		/**
		 * 初始化View对象
		 */
		initViews();
		/**
		 * 绑定按钮监听器
		 */
		initEventListener();
	}

	/**
	 * 初始化View对象
	 */
	private void initViews() {

		mreturn = (TextView) findViewById(R.id.textView_Return);

		next = (TextView) findViewById(R.id.textView_account_Next);

		accountName = (EditText) findViewById(R.id.editText_email);

		password = (EditText) findViewById(R.id.editText_password);

		surepassword = (EditText) findViewById(R.id.editText_surepassword);
	}

	/**
	 * 绑定按钮监听器
	 */
	private void initEventListener() {
		// 返回按钮
		mreturn.setOnClickListener(this);
		// 下一页面
		next.setOnClickListener(this);

		next.addTextChangedListener(new AccountNameTextWatcher());
		
		accountName.addTextChangedListener(new  AccountNameTextWatcher());
		password.addTextChangedListener(new  PasswordTextWatcher());
		surepassword.addTextChangedListener(new  SurePasswordTextWatcher());
	}

	@Override
	public void onClick(View v) {
		Intent i = null;
		if (v == mreturn) {
			i = new Intent(
					this,
					com.wildwolf.project.wildwolf.ui.wizard.AccountActivity.class);
			finish();
		} else if (v == next) {
			//
			i = new Intent(this, IntroductionActivity.class);
			i.putExtra(AppConfig.REGISTERKEY.ENCOUNTER_REGISTER_ACCOUNTNAME, accountName.getText().toString());
			i.putExtra(AppConfig.REGISTERKEY.ENCOUNTER_REGISTER_PASSWORD, password.getText().toString());
		}
		startActivity(i);
	}
	/**
	 * 
	 * @param accountName
	 */
	private void verifyAccountName(String accountName) {
		
		isLegalAccountName = !TextUtils.isEmpty(accountName);
		
	}
	


	/**
	 * 
	 * @param accountName
	 */
	private void verifyPassword(String password,String surepassword) {
		isLegalPassword = !TextUtils.isEmpty(password);
		if(isLegalPassword){
			isLegalSurePassword = !TextUtils.isEmpty(surepassword);
		}
		if(isLegalPassword&&isLegalSurePassword){
			isLegalPassword = isLegalSurePassword = password.equals(surepassword);
		}
		
	}

	/**
	 * 用户名
	 * 
	 * @author LEON
	 * 
	 */
	private class AccountNameTextWatcher implements TextWatcher {

		@Override
		public void afterTextChanged(Editable editable) {
			
			verifyAccountName(editable.toString());
			
			next.setEnabled(isLegalAccountName && isLegalPassword&& isLegalSurePassword);
		}

		@Override
		public void beforeTextChanged(CharSequence charSequence, int start,
				int count, int after) {
		}

		@Override
		public void onTextChanged(CharSequence charSequence, int start,
				int before, int count) {

		}
	}

	/**
	 * 
	 * @author LEON
	 * 
	 */
	private class PasswordTextWatcher implements TextWatcher {

		@Override
		public void afterTextChanged(Editable editable) {
			verifyPassword(editable.toString(),surepassword.getText().toString());
			next.setEnabled(isLegalAccountName && isLegalPassword&& isLegalSurePassword);
		}

		@Override
		public void beforeTextChanged(CharSequence charSequence, int start,int count, int after) {
		}

		@Override
		public void onTextChanged(CharSequence charSequence, int start,
				int before, int count) {

		}
	}
	/**
	 * 
	 * @author LEON
	 * 
	 */
	private class SurePasswordTextWatcher implements TextWatcher {

		@Override
		public void afterTextChanged(Editable editable) {
			verifyPassword(password.getText().toString(),editable.toString());
			next.setEnabled(isLegalAccountName && isLegalPassword&& isLegalSurePassword);
		}

		@Override
		public void beforeTextChanged(CharSequence charSequence, int start,
				int count, int after) {
		}

		@Override
		public void onTextChanged(CharSequence charSequence, int start,
				int before, int count) {

		}
	}
}
